<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use think\facade\View;
use app\admin\BaseController;
use app\admin\model\AdminMenu;
use app\admin\model\Themes;
use app\admin\model\Config as ConfigModel;
/**
 * 配置管理
 */
class Config extends BaseController
{
    /**
     * 配置中心
     */
    public function index()
    {
    	$menu  = [];
        $index = array_search($this->request->path, array_column($this->request->menu, 'path'));
        if ($index !== false) {
            $pid = $this->request->menu[$index]['id'];
        	foreach ($this->request->menu as $key => $val) {
        	    if ($pid == $val['pid']) {
        	        array_push($menu, $val);
        	    }
        	}
        	View::assign('menu', $menu);
        }
        return View::fetch();
    }
    
    /**
     * 基础配置
     */
    public function system()
    {
        if ($this->request->isPost()) {
            $input = input('post.');
            $msg   = ConfigModel::setVal('system','基础配置', $input['config']);
            return json($msg);
        } else {
            View::assign('config', ConfigModel::getVal('system'));
            return View::fetch();
        }
    }
    
    /**
     * 主题配置
     */
    public function theme()
    {
        if ($this->request->isPost()) {
            $input = input('post.');
            Themes::update(['config' => $input['config']], ['name' => theme()]);
            cache('theme_'.theme(), null);
            return json(['status' => 'success', 'message' => '修改成功']);
        } else {
            View::assign([
                'config' => Themes::where('name', theme())->field('config')->find(),
                'themes' => Themes::select(), 
                'system' => ConfigModel::getVal('system')
            ]);
            return View::fetch();
        }
    }
    
    /**
     * 邮箱配置
     */
    public function email()
    {
        if ($this->request->isPost()) {
            $input = input('post.');
            $msg   = ConfigModel::setVal('email','邮箱配置', $input['config']);
            return json($msg);
        } else {
            View::assign('config', ConfigModel::getVal('email'));
            return View::fetch();
        }
    }
    
    /**
     * 会员注册配置
     */
    public function register()
    {
        if ($this->request->isPost()) {
            $input = input('post.');
            $msg   = ConfigModel::setVal('register','会员注册配置', $input['config']);
            return json($msg);
        } else {
            View::assign('config', ConfigModel::getVal('register'));
            return View::fetch();
        }
    }
    
    /**
     * 上传限制
     */
    public function upload()
    {
        if ($this->request->isPost()) {
            $input = input('post.');
            file_put_contents(root_path().'app/api/config/upload.php', "<?php\nreturn ".var_export($input['config']['api'],true).";");
            file_put_contents(root_path().'app/admin/config/upload.php', "<?php\nreturn ".var_export($input['config']['admin'],true).";");
            return json(['status' => 'success', 'message' => '修改成功']);
        } else {
            $config = [
                'admin' => include(root_path().'app/admin/config/upload.php'),
                'api'   => include(root_path().'app/api/config/upload.php')
            ];
            View::assign('config', $config);
            return View::fetch();
        }
    }
}
